import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import java.sql.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 
 */

/**
 * @author Mayank
 *
 */
public class BarChart {
	
	Connection con=null;
	Date date;
	

    private static Log log = LogFactory.getLog(BarChart.class);

    private List _dates;

    private String _text;
    private Date _firstDate;
    private Date _secondDate;

    public List getDates()
    {
        if(_dates == null)
        {
            _dates = new ArrayList();

            for(int i=0; i<3; i++)
                _dates.add(new DateHolder());
        }

        return _dates;
    }

    public void setDates(List dates)
    {
        _dates = dates;
    }

    public String getText()
    {
        return _text;
    }

    public void setText(String text)
    {
        _text = text;
    }

    public Date getFirstDate()
    {
        return _firstDate;
    }

    public void setFirstDate(Date firstDate)
    {
        _firstDate = firstDate;
    }

    public Date getSecondDate()
    {
        return _secondDate;
    }

    public void setSecondDate(Date secondDate)
    {
        _secondDate = secondDate;
    }

    public String submitMethod()
    {
        log.info("submit method called");

        return "submit";
    }

	public BarChart() {
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public DefaultCategoryDataset getBarDataset() {
		DefaultCategoryDataset categoryDataSet;

		// row keys...
		String series1 = "First";
	//	String series2 = "Second";
	//	String series3 = "Third";
		// column keys...
		String category1 = "A";
		String category2 = "B";
		String category3 = "C";
		String category4 = "D";
		String category5 = "E";

		// create the dataset...
		categoryDataSet = new DefaultCategoryDataset();
		categoryDataSet.addValue(1.5, series1, category1);
		categoryDataSet.addValue(3.7, series1, category2);
		categoryDataSet.addValue(3.2, series1, category3);
		categoryDataSet.addValue(4.5, series1, category4);
		categoryDataSet.addValue(7.0, series1, category5);
	/*	categoryDataSet.addValue(5.0, series2, category1);
		categoryDataSet.addValue(7.0, series2, category2);
		categoryDataSet.addValue(6.0, series2, category3);
		categoryDataSet.addValue(8.0, series2, category4);
		categoryDataSet.addValue(4.0, series2, category5);
		categoryDataSet.addValue(4.0, series3, category1);
		categoryDataSet.addValue(3.0, series3, category2);
		categoryDataSet.addValue(2.0, series3, category3);
		categoryDataSet.addValue(3.0, series3, category4);
		categoryDataSet.addValue(6.0, series3, category5);
		*/
		return categoryDataSet;
	}
	
	public DefaultPieDataset getPieDataSet() {
		
		DefaultPieDataset pieDataSet = new DefaultPieDataset();
		pieDataSet.setValue("A",52);
		pieDataSet.setValue("B", 21);
		pieDataSet.setValue("C", 12); 
		pieDataSet.setValue("D", 15);
		return pieDataSet;
	}
	
	public DefaultPieDataset getPieData() {
		
		DefaultPieDataset pieDataSet = new DefaultPieDataset();
		ResultSet rs;
		try{
			rs=connect();
			while (rs.next())
				pieDataSet.setValue("A",52);
			
			con.close();
			return pieDataSet;
		}
		catch(SQLException sq)
		{
			System.out.println("exception2 " +sq);
			return pieDataSet;		
		}
	//	pieDataSet.setValue("B", 21);
	//	pieDataSet.setValue("C", 12); 
	//	pieDataSet.setValue("D", 15);
		
	}
	
	public DefaultCategoryDataset getConnectData() {
		
		DefaultCategoryDataset categoryDataSet=null;
	//	Connection con=null;
	//	String name=null;
		String topic=null;
		double score;
		String email=null;
		try{
	/*		Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/data","root","822669");
			Statement stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery("select * from student_scores");
		*/	ResultSet rs=connect();
			categoryDataSet = new DefaultCategoryDataset();
			while (rs.next())
			{
			//	name=rs.getString(1);
				topic=rs.getString(2);
				score=rs.getDouble(3);
				email=rs.getString(4);
				categoryDataSet.addValue(score,topic,email);
			}
			con.close();
			return categoryDataSet;
		}
	/*	catch(ClassNotFoundException cn)
		{
			System.out.println("exception " +cn);
			return categoryDataSet;
		}
	*/	catch(SQLException sq)
		{
			System.out.println("exception2 " +sq);
			return categoryDataSet;	
		}
	}
	
	public ResultSet connect() {
		
		ResultSet rs=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/data","root","822669");
			Statement stmt=con.createStatement();
			rs=stmt.executeQuery("select * from student_scores");
			return rs;
		}
		catch(ClassNotFoundException cn)
		{
			System.out.println("exception " +cn);
		//	return categoryDataSet;
			return rs;
		}
		catch(SQLException sq)
		{
			System.out.println("exception2 " +sq);
		//	return categoryDataSet;	
			return rs;
		}	
	}
}
